package com.lyf.emall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyf.emall.bean.*;
import com.lyf.emall.mapper.OrderMapper;
import com.lyf.emall.service.OrderService;
import com.lyf.emall.utils.IdUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author Lfy
 * @Date 2022/4/20-14:54
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public ResponseResult getAllOrder(Page page) {
        if (page.getPageNo() <= 0){
            return new ResponseResult(400, "页码不能越界");
        }

        List<OrderList> orderLists = orderMapper.queryAllOrder();
        PageHelper.startPage(page.getPageNo(), page.getPageNum());

        PageInfo<OrderList> pageInfo = new PageInfo<>(orderLists);
        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "获取订单列表成功", map);
    }

    @Transactional
    @Override
    public ResponseResult deliverGoods(OrderList orderList) {
        orderList.setStatus(3);
        orderList.setConsignTime(new Timestamp(System.currentTimeMillis()));

        orderMapper.updateOrderShipping(orderList);
        orderMapper.updateOrderStatus(orderList);
        return new ResponseResult(200, "发货成功");
    }

    @Override
    public ResponseResult updateRemark(String orderId, String buyerMessage) {
        Integer updateOrderRemark = orderMapper.updateOrderRemark(orderId, buyerMessage);
        if (updateOrderRemark <= 0){
            return new ResponseResult(400, "修改备注失败");
        }
        return new ResponseResult(200, "修改备注成功");
    }


    @Override
    public ResponseResult cancelOrder(OrderList orderList) {
        orderList.setStatus(6);

        Integer updateOrderStatus = orderMapper.updateOrderStatus(orderList);
        if (updateOrderStatus <= 0){
            return new ResponseResult(400, "取消订单失败");
        }
        return new ResponseResult(200, "取消订单成功");
    }

    @Override
    public ResponseResult getOrderInfo(String orderId) {
        OrderItem orderItem = orderMapper.queryOrderItemByOrderId(orderId);
        System.out.println(orderItem);
        OrderShipping orderShipping = orderMapper.queryOrderShippingByOrderId(orderId);
        System.out.println(orderShipping);
        String address = "";
        if (orderShipping.getReceiverState() != null){
            address = address + orderShipping.getReceiverState();
        }
        if (orderShipping.getReceiverCity() != null){
            address = address + orderShipping.getReceiverCity();
        }
        if (orderShipping.getReceiverDistrict() != null){
            address = address + orderShipping.getReceiverDistrict();
        }
        if (orderShipping.getReceiverAddress() != null){
            address = address + orderShipping.getReceiverAddress();
        }

        Map<String, Object> map = new HashMap<>();
        map.put("orderItem", orderItem);
        map.put("orderShipping", orderShipping);
        map.put("address", address);
        return new ResponseResult(200, "获取订单信息成功", map);
    }
}
